//
//  YIem_Explore_Detail_View.m
//  B段_开眼
//
//  Created by YIem on 16/4/29.
//  Copyright © 2016年 Howie. All rights reserved.
//

#define ACTION_MARGIN 80 //
#define SCALE_STRENGTH 4 // 收缩
#define SCALE_MAX .93
#define ROTATION_MAX 1 // 最大旋转
#define ROTATION_STRENGTH 320 // 旋转力量
#define ROTATION_ANGLE M_PI/8 // 最大旋转角度


#import "YIem_Explore_Detail_View.h"

@interface YIem_Explore_Detail_View ()

@property (nonatomic, strong) UIImageView *topImageV;
@property (nonatomic, strong) UIView *imageView;
@property (nonatomic, strong) UIImageView *xiaImageV;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *subTitleLabel;
@property (nonatomic, strong) UILabel *contrlLabel;
@property (nonatomic, strong) NSData *imageData;
@end
@implementation YIem_Explore_Detail_View {
    CGFloat xFromCenter;
    CGFloat yFromCenter;
}
@synthesize panGestureRecognizer;
@synthesize information;


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupView];
        
        
//    information = [[UILabel alloc] initWithFrame:CGRectMake(97, 50, 200, 100)];
//        information.text = @"占位符";
//        information.textColor = [UIColor whiteColor];
//        self.backgroundColor = [UIColor colorWithRed:0.22 green:0.24 blue:0.27 alpha:1];
        
        // 头部
        self.topImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 100)];
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.textColor = [UIColor whiteColor];
//        self.titleLabel.font = [UIFont systemFontOfSize:20];
        self.titleLabel.font = [UIFont systemFontOfSize:18 weight:10];

        self.subTitleLabel = [[UILabel alloc] init];
        self.subTitleLabel.textAlignment = NSTextAlignmentCenter;
        self.subTitleLabel.textColor = [UIColor whiteColor];
        self.subTitleLabel.font = [UIFont systemFontOfSize:17];

       
        // 下部
        self.xiaImageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.topImageV.frame.origin.y + self.topImageV.frame.size.height, self.topImageV.frame.size.width, self.frame.size.height - self.topImageV.frame.size.height)];
        self.contrlLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.xiaImageV.frame.origin.x + 5, self.xiaImageV.frame.origin.y + 5, self.xiaImageV.frame.size.width - 10,  self.xiaImageV.frame.size.height - 10)];
        self.contrlLabel.numberOfLines = 0;
        self.contrlLabel.textColor = [UIColor colorWithRed:0.7782 green:0.8044 blue:0.7796 alpha:1.0];
        self.contrlLabel.font = [UIFont systemFontOfSize:14];
        
        
        
        
        panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(beingDragged:)];
        [self addGestureRecognizer:panGestureRecognizer];
        
        
        // 添加
        
//        [self addSubview:self.xiaImageV];
//        [self addSubview:information];
        
    }
    return self;
}


- (void)setModel:(YIem_ExploreCellModel *)model
{
    _model = model;
    [self.topImageV sd_setImageWithURL:[NSURL URLWithString:model.feed]];
    [self addSubview:self.topImageV];
    self.imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 100)];
    self.imageView.backgroundColor = [UIColor blackColor];
    self.imageView.alpha = 0.3;
    [self.topImageV addSubview:self.imageView];
    
    self.titleLabel.text = model.title;
    [self.topImageV addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.topImageV);
        make.size.mas_equalTo(CGSizeMake(self.topImageV.frame.size.width, 30));
    }];
    NSString *timeStr = model.date;
    long long int date1 = (long long int)[timeStr intValue];
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:date1];
    NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
    [dateForm setDateFormat:@"MM '' dd '''' "];
    
    NSString *dateS = [dateForm stringFromDate:date];

    self.subTitleLabel.text = [NSString stringWithFormat:@"#%@ / %@", model.category, dateS];
    [self.topImageV addSubview:self.subTitleLabel];
    [self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(self.topImageV.frame.size.width, 30));
        
        
        make.top.equalTo(self.titleLabel).with.offset(40);
        make.centerX.equalTo(self.topImageV);
//        make.center.equalTo(self.topImageV);
    }];
    
    [self.xiaImageV sd_setImageWithURL:[NSURL URLWithString:model.blurred]];
    [self addSubview:self.xiaImageV];
    self.contrlLabel.text = model.des;
    [self addSubview:self.contrlLabel];
    

        NSError *ERROR;
        self.imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.blurred] options:NSDataReadingMappedIfSafe error:&ERROR];
        

    self.myBlock([UIColor colorWithPatternImage:[UIImage imageWithData:self.imageData]]);

   
}
- (void)setupView
{
    self.layer.cornerRadius = 4;
    self.layer.shadowRadius = 6;
    self.layer.shadowOpacity = 0.5;
    self.layer.shadowOffset = CGSizeMake(3, 3);
}

-(void)beingDragged:(UIPanGestureRecognizer *)gestureRecognizer
{
    xFromCenter = [gestureRecognizer translationInView:self].x; // 正确
    yFromCenter = [gestureRecognizer translationInView:self].y; // 取消
    
    
    switch (gestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:
        {
            self.originalPoint = self.center;
            break;
        }
            
        case UIGestureRecognizerStateChanged:{
            CGFloat rotationStrength = MIN(xFromCenter /ROTATION_STRENGTH, ROTATION_MAX); // 旋转
            CGFloat rotationAnger = (CGFloat)(ROTATION_ANGLE * rotationStrength);// 变化的弧度
            CGFloat scale = MAX(1 - fabsf(rotationStrength) / SCALE_STRENGTH, SCALE_MAX);
            self.center = CGPointMake(self.originalPoint.x + xFromCenter, self.originalPoint.y + yFromCenter);
            CGAffineTransform transform = CGAffineTransformMakeRotation(rotationAnger);
            CGAffineTransform scaleTransform = CGAffineTransformScale(transform, scale, scale);
            self.transform = scaleTransform;
            [self updateOverlay:xFromCenter];
            break;
        }
        case UIGestureRecognizerStateEnded:{
            [self afterSwipeAction];
            break;
        }
            case UIGestureRecognizerStatePossible:
            break;
            case UIGestureRecognizerStateCancelled:
            break;
            case UIGestureRecognizerStateFailed:
            break;
        default:
            break;
    }
}
// 判断 左右
-(void)updateOverlay:(CGFloat)distance
{
//    if (distance > 0) {
//        overlayView.mode = GGOverlayViewModeRight;
//    } else {
//        overlayView.mode = GGOverlayViewModeLeft;
//    }
//    
//    overlayView.alpha = MIN(fabsf(distance)/100, 0.4);
}
// 松开
- (void)afterSwipeAction
{
//    if (xFromCenter > ACTION_MARGIN) {
//        [self rightAction];
//
//    }else
        if (xFromCenter < -ACTION_MARGIN) {
        [self leftAction];
            self.myBlock([UIColor colorWithPatternImage:[UIImage imageWithData:self.imageData]]);

    } else { //%%% resets the card
        [UIView animateWithDuration:0.2
                         animations:^{
                             self.center = self.originalPoint;
                             self.transform = CGAffineTransformMakeRotation(0);
//                             overlayView.alpha = 0;
                         }];
    }
}
// 右
-(void)rightAction
{
    CGPoint finishPoint = CGPointMake(500, 2*yFromCenter +self.originalPoint.y);
    [UIView animateWithDuration:0.15
                     animations:^{
                         self.center = finishPoint;
                     }];
    NSLog(@"YES");
}
// 左
-(void)leftAction
{
    CGPoint finishPoint = CGPointMake(-500, 2*yFromCenter +self.originalPoint.y);
    [UIView animateWithDuration:0.15
                     animations:^{
                         self.center = finishPoint;
                     }];
    NSLog(@"NO");
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
